package com.sinog.basic.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.basic.mapper.ResourceMapper;
import com.sinog.basic.mapper.RoleResMapper;
import com.sinog.basic.service.RoleResService;
import com.sinog.core.domain.RoleRes;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 角色管理实现
 * @author -
 * @date Created in 2019年07月09日  15点28分
 */
@Service
public class RoleResServiceImpl extends ServiceImpl<RoleResMapper,RoleRes> implements RoleResService {

    @Autowired
    private RoleResMapper roleResMapper;
    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 获取角色资源列表
     * @param roleid roleid
     * @return String
     */
    @Override
    public String getRoleResourcesTree(String roleid) {
        List<Map<String,String>> list = resourceMapper.getRoleResourcesTree(roleid);
        return JSON.toJSONString(list);
    }

    /**
     * 保存角色资源列表
     * @param resMap resMap
     * @param roleid roleid
     * @param userName userName
     * @return Result
     */
    @Override
    public Result saveRoleResources(Map<String,String> resMap,String roleid,String userName) {
        String resids = resMap.get("resids");
        // 先删除 再添加
        int i = roleResMapper.deleteRoleResources(roleid);
        if(Constants.NumberStr.NUM_STR_FU_ONE.equals(resids)) {
            return Result.success(String.valueOf(i));
        }
        Map<String,Object> map = new HashMap<>(16);
        map.put("roleid",roleid);
        map.put("optime",new Date());
        map.put("opid",userName);
        List<String> res = Arrays.asList(resids.split(","));
        int i1 = roleResMapper.saveRoleResources(map,res);
        return Result.success(i + i1);
    }
}